package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;
import com.qiantu.phone.widget.EditTextLayout;
import com.lxj.xpopup.core.BottomPopupView;

/**
 * @Description: 设备操作码添加

 * @return:
 * @Author:
 * @Time:
 */
public class BottomCodeAddDialog extends BottomPopupView {
    private DialogClickListener mDialogClickListener;
    private TextView saveButton;
    private TextView cancelButton;
    private EditTextLayout inputText;


    public BottomCodeAddDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.video_code_dialog;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        saveButton = findViewById(R.id.btn_ok);
        cancelButton = findViewById(R.id.btn_cancel);
        inputText = findViewById(R.id.edit_name);
        BottomCodeAddDialog.MyOnClickListener myOnClickListener = new BottomCodeAddDialog.MyOnClickListener();
        saveButton.setOnClickListener(myOnClickListener);
        cancelButton.setOnClickListener(myOnClickListener);
        String text = inputText.mEditText.getText().toString();
        enableButton(text);
        inputText.mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 在文本变化时执行的操作
                String text = inputText.mEditText.getText().toString();
                enableButton(text);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }



    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    private void enableButton(String input){
        // 对获取到的文本进行操作
        if(!TextUtils.isEmpty(input)){
            inputText.getEditText().setSelection(inputText.getText().length());
            saveButton.setEnabled(true);
        }else{
            saveButton.setEnabled(false);
        }
    }

    private class MyOnClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_ok://保存
                    String text = inputText.mEditText.getText().toString();
                    if(mDialogClickListener != null){
                        mDialogClickListener.onConfirmClick(text);
                    }

                    break;
                case R.id.btn_cancel://取消
                    dismiss();
                    break;
            }
        }
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(String code);
    }

}
